import 'package:sil_utils_package/sil_utils_package.dart';
import 'dart:convert' as convert;

//游戏类型
enum GameType {
  h5,
  cps,
  jieji,
}

class GameEntity {
  String? id;
  String? remark;
  String? tagId;
  String? recommId;
  String? briefIntro;
  String? url;
  String? appName;
  String? authorName;
  String? packageName;
  String? commentCount;
  String? iconUrl;
  String? play;
  String? score;
  double? scoreDouble;
  String? scoreStr;
  String? fileSize;
  String? rating;
  String? description;
  List<String>? imageUrls;
  String? screen;
  String? version;
  String? bigBanner;
  String? apkUrl;
  String? hots;
  String? url_tag;
  String? reserve;
  String? isCoin;
  String? moniqiurl;
  String? enableClick;
  String? initial_load;

  /**扣除金币数 */
  String? score_ratio;
  String? score_min;
  String? score_max;

  /**金手指兑换分 */
  String? score_gold;

  /**金手指 */
  String? goldfinger;
  String? usages;
  String? rules;
  String? thumb;
  String? content;
  String? keywords;

  /** h5：null，""; 0:CPS手游; 街机；有值的时候 */
  String? moniqi;
  String? emuname;
  String? newbaoname;
  String? newdownurl;
  String? downUrl;
  String? presskey;
  String? video_url;
  String? emusupport;
  String? isnetwork;
  String? jsurl;
  String? language;
  String? savePath;
  List<String>? keywordsList;
  List<String>? urlTagList;
  String? urlTag;
  String? downPer;
  String? downState;
  String? jiejiDownedSize;
  String? jiejiDownedToal;
  String? type; //类型 [0:默认,1:街机,2:H5,3:需要下载的安装包apk]

  /**游戏分类名称 */
  String? tagIdName;
  /**h5下载地址 */
  String? url_h5;
  /**扩展 */
  Map<String, dynamic>? extend;

  /**app下载次数 */
  String? appDownCount;

  GameEntity(
      {this.id,
      this.enableClick,
      this.initial_load,
      this.savePath,
      this.moniqiurl,
      this.downPer,
      this.jiejiDownedSize,
      this.jiejiDownedToal,
      this.downState,
      this.remark,
      this.tagId,
      this.recommId,
      this.briefIntro,
      this.downUrl,
      this.url,
      this.appName,
      this.authorName,
      this.packageName,
      this.commentCount,
      this.iconUrl,
      this.play,
      this.score,
      this.scoreStr,
      this.scoreDouble,
      this.fileSize,
      this.apkUrl,
      this.rating,
      this.video_url,
      this.description,
      this.imageUrls,
      this.screen,
      this.version,
      this.bigBanner,
      this.hots,
      this.url_tag,
      this.reserve,
      this.isCoin,
      this.score_ratio,
      this.score_min,
      this.score_max,
      this.usages,
      this.rules,
      this.thumb,
      this.content,
      this.keywords,
      this.moniqi,
      this.emuname,
      this.newbaoname,
      this.newdownurl,
      this.presskey,
      this.emusupport,
      this.isnetwork,
      this.jsurl,
      this.language,
      this.keywordsList,
      this.urlTagList,
      this.urlTag,
      this.goldfinger,
      this.score_gold,
      this.tagIdName,
      this.type,
      this.url_h5,
      this.extend,
      this.appDownCount});

  /**
   * 返回游戏类型,类型 [0:默认,1:街机,2:H5,3:CPS]  
   */
  GameType getGameType() {
    if (SilTools.stringIsEmpty(type)) {
      return GameType.jieji;
    }

    switch (type) {
      case '1':
        return GameType.jieji;
      case '2':
        return GameType.h5;
      case '3':
        return GameType.cps;
      default:
        return GameType.jieji;
    }

    // if (SilTools.stringIsEmpty(moniqi)) {
    //   return GameType.h5;
    // } else if (moniqi == "0") {
    //   return GameType.cps;
    // } else {
    //   return GameType.jieji;
    // }
  }

  GameEntity.fromJson(Map<String, dynamic> json) {
    id = SilEntityLog.handleString(json, 'id');
    initial_load = SilEntityLog.handleString(json, 'initial_load');
    enableClick = SilEntityLog.handleString(json, 'enableClick');
    downPer = SilEntityLog.handleString(json, 'downPer');
    moniqiurl = SilEntityLog.handleString(json, 'moniqiurl');
    downUrl = SilEntityLog.handleString(json, 'downUrl');
    jiejiDownedSize = SilEntityLog.handleString(json, 'jiejiDownedSize');
    jiejiDownedToal = SilEntityLog.handleString(json, 'jiejiDownedToal');
    downState = SilEntityLog.handleString(json, 'downState');
    savePath = SilEntityLog.handleString(json, 'savePath');
    remark = SilEntityLog.handleString(json, 'remark');
    tagId = SilEntityLog.handleString(json, 'tagId');
    recommId = SilEntityLog.handleString(json, 'recommId');
    briefIntro = SilEntityLog.handleString(json, 'briefIntro');
    url = SilEntityLog.handleString(json, 'url');
    video_url = SilEntityLog.handleString(json, 'video_url');
    appName = SilEntityLog.handleString(json, 'appName');
    authorName = SilEntityLog.handleString(json, 'authorName');
    packageName = SilEntityLog.handleString(json, 'packageName');
    commentCount = SilEntityLog.handleString(json, 'commentCount');
    apkUrl = SilEntityLog.handleString(json, 'apkUrl');
    iconUrl = SilEntityLog.handleString(json, 'iconUrl');
    play = SilEntityLog.handleString(json, 'play');
    score = SilEntityLog.handleString(json, 'score');
    fileSize = SilEntityLog.handleString(json, 'fileSize');
    rating = SilEntityLog.handleString(json, 'rating');
    description = SilEntityLog.handleString(json, 'description');

    if (json.containsKey('extend')) {
      var tmp = json['extend'];
      if (!SilTools.stringIsEmpty(tmp)) {
        extend = convert.jsonDecode(tmp);
      }
    }

    if (json.containsKey('imageUrls')) {
      var tmp = json['imageUrls'];
      if (tmp is List) {
        imageUrls = json['imageUrls'].cast<String>();
      } else {
        imageUrls = [];
      }
    } else {
      imageUrls = [];
    }

    screen = SilEntityLog.handleString(json, 'screen');
    version = SilEntityLog.handleString(json, 'version');
    bigBanner = SilEntityLog.handleString(json, 'bigBanner');
    hots = SilEntityLog.handleString(json, 'hots');
    url_tag = SilEntityLog.handleString(json, 'url_tag');
    reserve = SilEntityLog.handleString(json, 'reserve');
    isCoin = SilEntityLog.handleString(json, 'isCoin');
    score_ratio = SilEntityLog.handleString(json, 'score_ratio');
    score_min = SilEntityLog.handleString(json, 'score_min');
    score_max = SilEntityLog.handleString(json, 'score_max');
    usages = SilEntityLog.handleString(json, 'usages');
    rules = SilEntityLog.handleString(json, 'rules');
    thumb = SilEntityLog.handleString(json, 'thumb');
    content = SilEntityLog.handleString(json, 'content');
    keywords = SilEntityLog.handleString(json, 'keywords');
    moniqi = SilEntityLog.handleString(json, 'moniqi');
    emuname = SilEntityLog.handleString(json, 'emuname');
    newbaoname = SilEntityLog.handleString(json, 'newbaoname');
    newdownurl = SilEntityLog.handleString(json, 'newdownurl');
    presskey = SilEntityLog.handleString(json, 'presskey');
    emusupport = SilEntityLog.handleString(json, 'emusupport');
    isnetwork = SilEntityLog.handleString(json, 'isnetwork');
    jsurl = SilEntityLog.handleString(json, 'jsurl');
    language = SilEntityLog.handleString(json, 'language');
    score_gold = SilEntityLog.handleString(json, 'score_gold');
    goldfinger = SilEntityLog.handleString(json, 'goldfinger');
    tagIdName = SilEntityLog.handleString(json, 'tagIdName');
    type = SilEntityLog.handleString(json, 'type');
    url_h5 = SilEntityLog.handleString(json, 'url_h5');
    appDownCount = SilEntityLog.handleString(json, 'appDownCount');

    // 处理游戏标签
    urlTagList = [];
    if (!SilTools.stringIsEmpty(url_tag)) {
      urlTagList = url_tag!.split(",");
    }

    // 处理游戏关键字
    keywordsList = [];
    if (!SilTools.stringIsEmpty(keywords)) {
      keywordsList = keywords!.split(",");
    }

    urlTag = SilEntityLog.handleString(json, 'urlTag');

    scoreDouble = 0.0;
    scoreStr = "0.0";
    if (!SilTools.stringIsEmpty(score)) {
      scoreDouble = double.parse(score!);
      scoreStr = scoreDouble!.toStringAsFixed(1); // 保留小数点后一位
    }

    // 处理游戏大小
    if ((fileSize != null)) {
      num? fileSizeNum = num.tryParse(fileSize!);
      if (fileSizeNum != null) {
        String sizeString = "0.0";

        // 返回的是字节,处理一下
        if (fileSizeNum > 1024 * 1024) {
          sizeString = "${(fileSizeNum / 1024 * 1024).toStringAsFixed(2)}MB";
        } else if (fileSizeNum > 1024) {
          sizeString = "${(fileSizeNum / 1024).toStringAsFixed(2)}KB";
        }
        fileSize = sizeString;
      }
    }

    // 处理banner
    if (SilTools.stringIsEmpty(bigBanner)) {
      if (!SilTools.listIsEmpty(imageUrls)) {
        bigBanner = imageUrls![0];
      }
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['downPer'] = this.downPer;
    data['initial_load'] = this.initial_load;
    data['enableClick'] = this.enableClick;
    data['moniqiurl'] = this.moniqiurl;
    data['downUrl'] = this.downUrl;
    data['savePath'] = this.savePath;
    data['jiejiDownedSize'] = this.jiejiDownedSize;
    data['jiejiDownedToal'] = this.jiejiDownedToal;
    data['downState'] = this.downState;
    data['remark'] = this.remark;
    data['tagId'] = this.tagId;
    data['recommId'] = this.recommId;
    data['briefIntro'] = this.briefIntro;
    data['video_url'] = this.video_url;
    data['url'] = this.url;
    data['appName'] = this.appName;
    data['authorName'] = this.authorName;
    data['packageName'] = this.packageName;
    data['commentCount'] = this.commentCount;
    data['iconUrl'] = this.iconUrl;
    data['apkUrl'] = this.apkUrl;
    data['play'] = this.play;
    data['score'] = this.score;
    data['fileSize'] = this.fileSize;
    data['rating'] = this.rating;
    data['description'] = this.description;
    data['imageUrls'] = this.imageUrls;
    data['screen'] = this.screen;
    data['version'] = this.version;
    data['bigBanner'] = this.bigBanner;
    data['hots'] = this.hots;
    data['url_tag'] = this.url_tag;
    data['reserve'] = this.reserve;
    data['isCoin'] = this.isCoin;
    data['score_ratio'] = this.score_ratio;
    data['score_min'] = this.score_min;
    data['score_max'] = this.score_max;
    data['usages'] = this.usages;
    data['rules'] = this.rules;
    data['thumb'] = this.thumb;
    data['content'] = this.content;
    data['keywords'] = this.keywords;
    data['moniqi'] = this.moniqi;
    data['emuname'] = this.emuname;
    data['newbaoname'] = this.newbaoname;
    data['newdownurl'] = this.newdownurl;
    data['presskey'] = this.presskey;
    data['emusupport'] = this.emusupport;
    data['isnetwork'] = this.isnetwork;
    data['jsurl'] = this.jsurl;
    data['language'] = this.language;
    data['keywordsList'] = this.keywordsList;
    data['urlTagList'] = this.urlTagList;
    data['urlTag'] = this.urlTag;
    data['score_gold'] = this.score_gold;
    data['goldfinger'] = this.goldfinger;
    data['scoreDouble'] = this.scoreDouble;
    data['scoreStr'] = this.scoreStr;
    data['tagIdName'] = this.tagIdName;
    data['type'] = this.type;
    data['url_h5'] = this.type;
    data['extend'] = this.extend;
    data['appDownCount'] = this.appDownCount;

    return data;
  }
}
